<template>
  <div>
    <!-- firststart -->
    <div style="margin: 40px;margin-top: -5px;margin-bottom: 20px;">
      <div style="padding: 15px;padding-left: 30px;background-color: #fdb6a7;">
          <div style="">当前订单状态：<span style="color: #0eb0fd;">{{orderDetailList.status_name}}</span></div>
          <div style="margin-top: 5px;margin-bottom: -5px;">
              <el-button  v-if="orderDetailList.status == 2 && orderDetailList.delivery_method_name !='自提'"   @click="deliverGoods(orderDetailList)" type="primary" size="mini">立即发货</el-button>
              <el-button  v-if="orderDetailList.status == 1"  :disabled="true" type="primary" size="mini">立即发货</el-button>
              <el-button v-if="orderDetailList.status == 3 && orderDetailList.delivery_method_name ==='自提'" type="success" size="mini" @click="confirmReceive(orderDetailList.id)">确认收货</el-button>

              <el-button v-if="orderDetailList.status != 6 && orderDetailList.status != 5 && orderDetailList.status != 4"
                   @click="cancelOrder(orderDetailList.id)" type="text" style="color: red;">取消订单</el-button>
          </div>
      </div>
    </div>
    <div class="wrapBetween">
      <!-- 左边 -->
      <div class="contenLeft">
        <div class="title">买家信息</div>
        <div class="detailCss">
          <span class="detailSpan">收货姓名</span>
          <span v-if="address_info.consignee!='' ">{{address_info.consignee}}</span>
          <span v-else>未填写</span>
        </div>
        <div class="detailCss">
          <span class="detailSpan">手机号码</span>
          <span>{{address_info.phone}}</span>
        </div>
        <div class="detailCss">
          <span class="detailSpan">收货地址</span>
          <span v-if="address_info.province!=''">
            <span>{{address_info.province}}/{{address_info.city}}/{{address_info.area}}/{{address_info.address}}</span>
          </span>
          <span v-else>
            地址未填写
          </span>
        </div>
        <div class="title" style="margin-top: 20px;">物流信息</div>
        <div  v-if="orderDetailList.delivery_method_name !='自提'">
          <div class="detailCss">
            <span class="detailSpan">物流公司</span>
            <span v-if="orderDetailList.express!=null">{{orderDetailList.express}}</span>
            <span v-else>未填写</span>
          </div>
          <div class="detailCss">
            <span class="detailSpan">运单号</span>
            <span  class="wrapSpan" v-if="orderDetailList.express_num!=null">{{orderDetailList.express_num}}</span>
            <span v-else>未填写</span>
          </div>
        </div>
        <div class="detailCss">
          <span class="detailSpan">配送方式</span>
          <span>{{orderDetailList.delivery_method_name}}</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method == 'express_delivery' || orderDetailList.delivery_method == 'instant_delivery' ">
          <span class="detailSpan">运费</span>
          <span>{{orderDetailList.delivery_fee}}</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method_name !='自提'">
          <span class="detailSpan">发货时间</span>
          <span v-if="orderDetailList.express_time!=null">{{orderDetailList.express_time}}</span>
          <span v-else>尚未发货</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method_name =='自提'">
          <span class="detailSpan">收货时间</span>
          <span v-if="orderDetailList.receive_time!=null">{{orderDetailList.receive_time}}</span>
          <span v-else>尚未收货</span>
        </div>

      </div>
      <!-- 右边 -->
      <div class="contenLeft">
        <div class="title">订单信息</div>
         <div class="detailCss">
           <span class="detailSpan">订单号</span>
           <span class="wrapSpan">{{orderDetailList.order_num}}</span>
         </div>
         <div class="detailCss">
           <span class="detailSpan">订单状态</span>
           <span>{{orderDetailList.status_name}}</span>
         </div>
         <!-- <div class="detailCss">
           <span class="detailSpan">下单用户</span>
           <span>{{user_info.nickname}}</span>
         </div> -->
        <!-- <div class="detailCss">
           <span class="detailSpan">实付</span>
           <span>{{orderDetailList.pay_amount}}</span>
         </div> -->
        <!-- <div class="detailCss">
           <span class="detailSpan">优惠金额</span>
           <span>{{orderDetailList.coupon_reduce_price}}</span>
         </div> -->
         <div class="detailCss">
           <span class="detailSpan">所属门店</span>
           <span>{{orderDetailList.merchant_name}}</span>
         </div>
        <!-- <div class="detailCss">
           <span class="detailSpan">支付方式</span>
           <span v-if="orderDetailList.pay_way!=null">{{orderDetailList.pay_way}}</span>
           <span v-else>未支付</span>
         </div> -->
         <div class="detailCss">
           <span class="detailSpan">订单备注</span>
           <span v-if="orderDetailList.remark!=''">{{orderDetailList.remark}}</span>
           <span v-else>暂无备注</span>
         </div>
         <div class="detailCss">
           <span class="detailSpan">支付时间</span>
           <span v-if="orderDetailList.pay_time!=null">{{orderDetailList.pay_time}}</span>
           <span v-else>未支付</span>
         </div>
         <div class="detailCss" v-if="orderDetailList.status==6">
           <span class="detailSpan">取消时间</span>
           <span v-if="orderDetailList.updated_at!=null">{{
        orderDetailList.updated_at}}</span>
        </div>
        <div class="detailCss">
          <span class="detailSpan">创建时间</span>
          <span>{{orderDetailList.created_at}}</span>
        </div>

      </div>
    </div>
    <!-- firstend -->
    <!-- secondStart-->
    <div class="secondConten">
      <div class="contenBottem ">
        <div class="title" style="margin-bottom: 20px;">子订单信息</div>
        <div class="detailAll" :style="'justify-content:'+(number==0?'space-around':'flex-start')">
          <el-table :data="order_item_list" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            border style="width: 100%;">
            <el-table-column align="center" prop="id" label="序号" />
            <el-table-column align="center" prop="item_num" label="子订单号" />
            <el-table-column align="center" prop="goods_info.name" label="图片" >
              <template slot-scope="scope">
                  <div><img width="50%" :src="thumb_url[0].img_Url"/></div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="goods_info.name" label="商品名称" />
            <el-table-column align="center" prop="goods_info.specification" label="规格" />
            <!-- <el-table-column align="center" prop="item_num" label="规格编码" /> -->
            <el-table-column align="center" prop="goods_info.classify_name" label="商品类型" />
            <el-table-column align="center" prop="discount_price" label="单价" />
            <el-table-column align="center" prop="num" label="数量" />
            <el-table-column align="center" prop="discount_amount" label="优惠信息">
               <template slot-scope="scope">
                 <!--<div v-if="scope.row.discount_amount==0">无</div>
                 <div v-else>{{scope.row.discount_amount}}</div>-->
                 <div>{{orderDetailList.coupon_name?orderDetailList.coupon_name:'无'}}</div>
               </template>
            </el-table-column>
            <el-table-column align="center" prop="total_amount" label="总价" />
          </el-table>

          <!-- v-for end-->
        </div>
        <div style="text-align: right;padding: 20px;color: #000000;">实付款：<span style="color: red;">￥{{orderDetailList.pay_amount}}</span></div>
        <div style="display: flex;justify-content: flex-end;color: #b9afaf;font-size: 15px;">
            <div style="margin-right: 20px;">含邮费：{{orderDetailList.delivery_fee}}</div>
            <div style="">优惠金额：{{orderDetailList.coupon_reduce_price}}</div>
        </div>
      </div>
    </div>
    <!-- 添加模态款 -->
    <el-dialog title="设置发货信息" style="margin-top: 16vh !important;" :close-on-click-modal="false" :visible.sync="modelMsg">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form label-width="160px">
            <el-form-item label="订单序号">
              <el-input v-model="expressList.order_id" disabled></el-input>
            </el-form-item>
            <el-form-item label="快递公司/配送平台" :required="true">
              <el-input v-model="expressList.express" placeholder="请填写快递公司/配送平台"></el-input>
            </el-form-item>
            <el-form-item label="快递单号/配送单号" :required="true">
              <el-input v-model="expressList.express_num" placeholder="请填写快递单号/配送单号"></el-input>
            </el-form-item>

          </el-form>
          <div style="text-align: center;">
            <el-button type="success" @click="submitGoods(1)">直接发货</el-button>
            <el-button type="infor" @click="submitGoods(0)">保存草稿</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "detail",
    data() {
      return {
        order_id: "",
        orderDetailList: [],
        address_info: [],
        user_info: [],
        order_item_list: [],
        goods_info: [],
        thumb_url:[],
        number:"",
        totalSale:"",
        expressList: {
          order_id: "",
          express: "",
          express_num: "",
          is_shop: ""
        },
        modelMsg: false,
      }

    },
    created() {
      this.order_id = this.$route.query.row
    },
    mounted() {
      this.getOrderDetail()
    },
    watch: {
      //监听相同路由下参数变化的时候，从而实现异步刷新
      $route(to, from) {
        this.order_id = this.$route.query.row;
        this.getOrderDetail()
      }
    },
    methods: {
      //自提确认收货
     async confirmReceive(orderid){
        let params={
          order_id:orderid
        }
        await this.$confirm('确定要确认收货吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '关闭',
          type: 'success'
        }).then(() => {
          const loadingOne = this.$loading({
            lock: true,
            text: '取消中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$api.confirmReceive(params).then(res=>{
            console.log(res)
            if(res.data.level=="success"){
              this.$message.success(res.data.message)
              this.getOrderDetail()
              loadingOne.close()
            }else{
              this.$message.error(res.data.message)
              loadingOne.close()
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已关闭'
          });
        });

      },
      //直接发货
      submitGoods(is_shop) {
        /* is_shop 为0是保存草稿 , 为1直接发货 */
        this.expressList.is_shop = is_shop
        const loadingOne = this.$loading({
          lock: true,
          text: '保存中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        this.$api.setOrderShipInfo(this.expressList).then(res => {
          this.$message({
            type: res.data.level,
            message: res.data.message
          });
          if (res.data.level == "success") {
            window.location.reload()
            this.modelMsg = false
            loadingOne.close()
          } else {
            loadingOne.close()
            this.modelMsg = true
          }

        })
      },
      //打开弹窗
      deliverGoods(row) {
        console.log("dd")
        console.log(row)
        this.expressList.order_id = row.id
        this.modelMsg = true
        this.expressList.express = row.express
        this.expressList.express_num = row.express_num
        this.expressList.is_shop =""
      },
      //取消订单
      async cancelOrder(id) {
        let params = {
          "order_id": id
        }
        await this.$confirm('确定要取消此订单吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '关闭',
          type: 'warning'
        }).then(() => {
          const loadingOne = this.$loading({
            lock: true,
            text: '取消中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$api.cancelOrder(params).then(res => {
            console.log(res)
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getOrderDetail()
              loadingOne.close()
            } else {
              loadingOne.close()
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已关闭'
          });
        });

      },
      getOrderDetail() {

        this.$api.getOrderDetail({
          order_id: this.order_id
        }).then(res => {
          console.log(res)
          this.orderDetailList = res.data.data
          this.totalSale = this.orderDetailList.original_price-this.orderDetailList.discount_price
          this.address_info = this.orderDetailList.address_info
          this.user_info = this.orderDetailList.user_info
          this.order_item_list = this.orderDetailList.order_item_list
          this.order_item_list.forEach(item=>{
              this.number++
          })
          console.log(this.order_item_list)
          //计算子订单数量是奇数还是偶数
          this.number = this.number%2
          this.goods_info = this.order_item_list[0].goods_info
          console.log(this.goods_info)
          this.thumb_url = this.goods_info.thumb_url
          console.log(this.thumb_url)

        })
      }
    }
  }
</script>
<style scoped lang="scss">
  @import "../../../assets/css/detailCss.scss";
</style>
